package com.lp.biz.content.service.impl;
import java.time.LocalDateTime;

import com.github.pagehelper.PageHelper;
import com.lp.biz.content.mapper.BizCommentMapper;
import com.lp.biz.content.model.entity.BizComment;
import com.lp.biz.content.model.query.CommentQuery;
import com.lp.biz.content.model.vo.CreateComment;
import com.lp.biz.content.service.CommentService;
import com.lp.biz.system.service.SysService;
import com.lp.common.context.ContextHolder;
import com.lp.common.exception.BaseException;
import com.lp.common.exception.ErrorCodeEnum;
import com.lp.common.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.lp.common.constant.CommentConstant.TYPE_COMMENT;
import static com.lp.common.constant.CommentConstant.TYPE_REPLY;
import static com.lp.common.constant.CommonConstant.SURE;
import static com.lp.common.exception.ErrorCodeEnum.CONTENT_VIOLATION;

/**
 * @program: backend
 * @description:
 * @author: Ke.Song
 * @since: 2024-07-01 23:29:03
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private SysService sysService;

    @Autowired
    private BizCommentMapper commentMapper;

    @Override
    public CommonResult getCommentList(CommentQuery query) {
        PageHelper.startPage(query.getPage(), query.getSize());
        return CommonResult.restPage(commentMapper.getCommentList(query));
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteComment(Integer commentId) {
        BizComment comment = commentMapper.selectById(commentId);
        if (comment == null || comment.getIsDeleted().equals(SURE)) {
            throw new BaseException(ErrorCodeEnum.PARAMS_ERROR);
        }
        comment.setIsDeleted(SURE);
        commentMapper.updateById(comment);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void createReply(Integer commentId, CreateComment createComment) {
        BizComment comment = commentMapper.selectById(commentId);
        if (comment == null || comment.getIsDeleted().equals(SURE) || !comment.getType().equals(TYPE_COMMENT)) {
            throw new BaseException("评论不存在");
        }
        Integer memberId = ContextHolder.getContext().getMemberId();
        if (sysService.scanContent(createComment.getContent(), memberId)) {
            throw new BaseException(CONTENT_VIOLATION);
        }
        BizComment reply = new BizComment();
        reply.setMemberId(memberId);
        reply.setCommenteeId(comment.getMemberId());
        reply.setContent(createComment.getContent());
        reply.setType(TYPE_REPLY);
        reply.setContentId(comment.getContentId());
        reply.setContentType(comment.getContentType());
        reply.setParentId(commentId);
        reply.setBizCommentId(commentId);
        reply.setCreateAt(LocalDateTime.now());
        reply.setIsOfficial(SURE);
        commentMapper.insert(reply);
    }
}
